//
//  AddUserViewController.swift
//  TCMHealth
//
//  Created by 韩颖 on 2023/7/2.
//

import UIKit
import TextFieldEffects
import HXPHPicker
import AVFAudio
import RxSwift
import RxCocoa
import RxRelay


class AddUserViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var saveButton: UIButton!
    var photoView: SinaPhotoView!
    var assetArray = [PhotoAsset]()
    var diseaseDesTextView: LNGLimitTextView!
    var reloadData: (() -> Void)?
    var sectionNum:Int = 1
    var sectionRowDic: [Int : Int] = [:]
    var addUserSymptom: Bool = false
    let disposeBug = DisposeBag()
    var userInfo: UserInfo = UserInfo()
    var medicinePrintTitle: String = ""
    var medicinePrintDic: [String : String] = [:]
    let addMedicationVC = AddMedicationViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        configTableView()
    }
    
    func configTableView(){
        tableView.register(UINib.init(nibName: "AddUserHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "AddUserHeaderView")
        tableView.register(UINib.init(nibName: "UserSymptomCell", bundle: nil), forCellReuseIdentifier: "UserSymptomCell")
        tableView.register(UINib.init(nibName: "PrescribeCell", bundle: nil), forCellReuseIdentifier: "PrescribeCell")
        
        tableView.register(UINib.init(nibName: "AddMedicationCell", bundle: nil), forCellReuseIdentifier: "AddMedicationCell")
        tableView.register(UINib.init(nibName: "CommentCell", bundle: nil), forCellReuseIdentifier: "CommentCell")
    }
    
    @IBAction func addUser(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    
    @IBAction func closeAction(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    
    func addMedication() {
        addMedicationVC.modalPresentationStyle = .overFullScreen
        addMedicationVC.saveMedicines = { [weak self] medicineTName, medicineDic in
            self?.medicinePrintDic = medicineDic
            self?.medicinePrintTitle = medicineTName
            self?.tableView.reloadData()
        }
        self.present(addMedicationVC, animated: true, completion: nil)
    }
}


extension AddUserViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionNum
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 280
        }
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            let tableHeader = tableView.dequeueReusableHeaderFooterView(withIdentifier: "AddUserHeaderView") as! AddUserHeaderView
            Observable.combineLatest(tableHeader.userNameTF.rx.text.orEmpty, tableHeader.phoneTF.rx.text.orEmpty, tableHeader.ageTF.rx.text.orEmpty) {
                        textValue1, textValue2,textValue3 -> Bool in
                return textValue1.count > 0 && textValue2.count > 0 && textValue3.count > 0
                        }
                        .map { $0 }
                        .bind(to: saveButton.rx.isEnabled)
                        .disposed(by: disposeBug)
            return tableHeader
        }
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let userSymptomCell = tableView.dequeueReusableCell(withIdentifier: "UserSymptomCell", for: indexPath) as! UserSymptomCell
            userSymptomCell.updateCell = {
                tableView.reloadData()
            }
            return userSymptomCell
        }else if indexPath.row == 1 {
            let prescribeCell = tableView.dequeueReusableCell(withIdentifier: "PrescribeCell", for: indexPath) as! PrescribeCell
            prescribeCell.updateCell = {
                tableView.reloadData()
            }
            
            prescribeCell.addMedication = {
                self.addMedication()
            }
            
            prescribeCell.gotoMedicine = {
                self.addMedication()
            }
            prescribeCell.setPrintMedicines(medicinePrintTitle,medicines: medicinePrintDic)
            return prescribeCell
        }else {
            let commentCell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
            return commentCell
        }
        
    }
}

